#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->lineEdit->setPlaceholderText("input the added content");

    for(int i = 0;i<3;i++)
    {
        ui->tableWidget->insertColumn(i);
        ui->tableWidget->insertRow(i);
    }
    QStringList stringList{"name","gender","age"};
    ui->tableWidget->setHorizontalHeaderLabels(stringList);
//    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("name"));
//    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("gender"));
//    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("age"));

    ui->tableWidget->setItem(0,0,new QTableWidgetItem("Mike"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("male"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("18"));

    ui->tableWidget->setItem(1,0,new QTableWidgetItem("Brown"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("male"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("19"));

    ui->tableWidget->setItem(2,0,new QTableWidgetItem("Jean"));
    ui->tableWidget->setItem(2,1,new QTableWidgetItem("female"));
    ui->tableWidget->setItem(2,2,new QTableWidgetItem("22"));





}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_addRow_clicked()
{
    int rowPos = ui->tableWidget->currentRow();
    if(rowPos == -1)qDebug() << "have not chosen one";
    ui->tableWidget->insertRow(rowPos);
}

void Widget::on_pushButton_addColumn_clicked()
{
    QString HorizontalHeaderText = ui->lineEdit->text();
    int colPos = ui->tableWidget->currentColumn();
    if(colPos == -1||HorizontalHeaderText.isEmpty())
    {
        qDebug() << "have not chosen one";
        return ;
    }
    ui->lineEdit->clear();
    ui->tableWidget->insertColumn(colPos);
    ui->tableWidget->setHorizontalHeaderItem(colPos,new QTableWidgetItem(HorizontalHeaderText));
}

void Widget::on_pushButton_delRow_clicked()
{
    int rowPos = ui->tableWidget->currentRow();
    if(rowPos == -1)
    {
        qDebug() << "error";
        return ;
    }
    ui->tableWidget->removeRow(rowPos);
}

void Widget::on_pushButton_delColumn_clicked()
{
    int colPos = ui->tableWidget->currentColumn();
    if(colPos == -1)
    {
        qDebug() << "error";
        return ;
    }
    ui->tableWidget->removeColumn(colPos);
}
